package com.example.testapplication.fragment;

import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.testapplication.R;
import com.example.testapplication.adapter.CartMainAdapter;
import com.example.testapplication.adapter.ShoppingFragmentAdapter;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.entity.AddressEntity;
import com.example.testapplication.entity.Result;
import com.example.testapplication.entity.response.CartResponse;
import com.example.testapplication.entity.response.DefaultAddResponse;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class CartMainFragment extends BaseFragment{
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private final String[] mTitles = {"全部"};
    private ViewPager viewPager;
    private Button btn_order;
    private Button btn_clean;


    public static CartMainFragment newInstance() {
        CartMainFragment fragment = new CartMainFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_cart_main;
    }

    @Override
    protected void initView() {
        btn_order = mRootView.findViewById(R.id.btn_order);
        btn_clean = mRootView.findViewById(R.id.btn_clear);
        viewPager = mRootView.findViewById(R.id.fixedViewPagerCart);
    }

    @Override
    protected void initData() {
        btn_clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cart_clean();
            }
        });
        btn_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cart_order();
            }
        });
        fragments.add(CartFragment.newInstance());
        viewPager.setOffscreenPageLimit(fragments.size());
        viewPager.setAdapter(new CartMainAdapter(getFragmentManager(),mTitles,fragments));
    }
    private void cart_order() {
        HashMap<String,Object> params = new HashMap<>();
        Api.config(ApiConfig.USER_DEFAULT_ADDRESS,params).getRequest(getActivity(), new CallBack() {
            @Override
            public void onSuccess(String result) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.v("defaultaddress",result);
                        Gson gson = new Gson();
                        DefaultAddResponse res = gson.fromJson(result, DefaultAddResponse.class);
                        if(res.getCode() == 1){
                            int defaultid = res.getData().getId();
                            params.put("addressid",defaultid);
                            Api.config(ApiConfig.USER_ORDERING,params).postRequest(getActivity(), new CallBack(){

                                @Override
                                public void onSuccess(String result) {
                                    getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Gson gson = new Gson();
                                            Result resu = gson.fromJson(result, Result.class);
                                            if(resu.getCode()==1){
                                                //成功
                                            }else{
                                                //失败
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(IOException e) {
                                    //请求失败
                                }
                            });
                        }else{
                            showToastAsync("失败");
                        }
                    }
                });

            }

            @Override
            public void onFailure(IOException e) {

            }
        });
    }

    private void cart_clean() {
        HashMap<String,Object> params = new HashMap<>();
        Api.config(ApiConfig.USER_CLEANCART,params).deleteRequest(getActivity(), new CallBack() {
            @Override
            public void onSuccess(String result) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.v("---vnv",result);
                        Gson gson = new Gson();
                        Result res = gson.fromJson(result, Result.class);
                        if(res.getCode() == 1){
                            Log.v("nima-->",result);
                            showToastAsync("成功");
                        }else{
                            showToastAsync("失败");
                        }
                    }
                });

            }

            @Override
            public void onFailure(IOException e) {

            }
        });
    }
}
